Completed
Push — master ( 1c94db...55fe0d )
by Dongxin
23s
created

config.js ➔ onConfig   B

Complexity

Conditions 4
Paths 16385

Size

Total Lines 167

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 4
c 4
b 0
f 1
nc 16385
nop 1
dl 0
loc 167
rs 8.1935

1 Function

Rating   Name   Duplication   Size   Complexity  
D config.js ➔ ... ➔ document.addEventListener(ꞌclickꞌ) 0 105 13

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
// Copyright © 2017 Tangdongxin
2
3
// Permission is hereby granted, free of charge, to any person obtaining
4
// A copy of this software and associated documentation files (the "Software"),
5
// To deal in the Software without restriction, including without limitation
6
// The rights to use, copy, modify, merge, publish, distribute, sublicense,
7
// And/or sell copies of the Software, and to permit persons to whom the
8
// Software is furnished to do so, subject to the following conditions:
9
10
// The above copyright notice and this permission notice shall be included
11
// In all copies or substantial portions of the Software.
12
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21
22
// ===========================================
23
// READ CONFIG
24
// ===========================================
25
function onConfig (result) {
26
27
  if (!result) { // {{{
28
29
    dlog('No Config find');
30
    fontStyle = 'Consolas';
0 ignored issues
show
Bug introduced by
The variable fontStyle seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.fontStyle.
Loading history...
31
    fontSize = '14px';
0 ignored issues
show
Bug introduced by
The variable fontSize seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.fontSize.
Loading history...
32
    bgColor = '#FDF6E3';
0 ignored issues
show
Bug introduced by
The variable bgColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.bgColor.
Loading history...
33
    intColor = '#657A81';
0 ignored issues
show
Bug introduced by
The variable intColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.intColor.
Loading history...
34
    strColor = '#2AA198';
0 ignored issues
show
Bug introduced by
The variable strColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.strColor.
Loading history...
35
    keyColor = '#B58900';
0 ignored issues
show
Bug introduced by
The variable keyColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.keyColor.
Loading history...
36
    defaultColor = '#586E75';
0 ignored issues
show
Bug introduced by
The variable defaultColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.defaultColor.
Loading history...
37
38
    strictOnly = false;
0 ignored issues
show
Bug introduced by
The variable strictOnly seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.strictOnly.
Loading history...
39
    hideDetails = false;
0 ignored issues
show
Bug introduced by
The variable hideDetails seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.hideDetails.
Loading history...
40
    dontBeatify = false;
0 ignored issues
show
Bug introduced by
The variable dontBeatify seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.dontBeatify.
Loading history...
41
    isHighlight = false;
0 ignored issues
show
Bug introduced by
The variable isHighlight seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.isHighlight.
Loading history...
42
    isDebug = false;
0 ignored issues
show
Bug introduced by
The variable isDebug seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.isDebug.
Loading history...
43
44
    strLength = 300;
0 ignored issues
show
Bug introduced by
The variable strLength seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.strLength.
Loading history...
45
    return;
46
47
  }// }}}
48
49
  if (result && result[0]) { // {{{
50
51
    fontStyle = result[0].fontStyle || 'Consolas';
52
    fontSize = result[0].fontSize || '14px';
53
    bgColor = result[0].bgColor || '#FDF6E3';
54
    intColor = result[0].intColor || '#657A81';
55
    strColor = result[0].strColor || '#2AA198';
56
    keyColor = result[0].keyColor || '#B58900';
57
    defaultColor = result[0].defaultColor || '#586E75';
58
59
    strictOnly = result[0].strictOnly || false;
60
    hideDetails = result[0].hideDetails || false;
61
    dontBeatify = result[0].dontBeatify || false;
62
    isHighlight = result[0].isHighlight || false;
63
    isDebug = result[0].isDebug || false;
64
    strLength = result[0].strLength || 300;
65
66
  } else {
67
68
    fontStyle = result.fontStyle || 'Consolas';
69
    fontSize = result.fontSize || '14px';
70
    bgColor = result.bgColor || '#FDF6E3';
71
    intColor = result.intColor || '#657A81';
72
    strColor = result.strColor || '#2AA198';
73
    keyColor = result.keyColor || '#B58900';
74
    defaultColor = result.defaultColor || '#586E75';
75
76
    strictOnly = result.strictOnly || false;
77
    hideDetails = result.hideDetails || false;
78
    dontBeatify = result.dontBeatify || false;
79
    isHighlight = result.isHighlight || false;
80
    isDebug = result.isDebug || false;
81
    strLength = result.strLength || 300;
82
83
  }// }}}
84
85
  document.addEventListener('click', function (e) {
86
87
    dlog(document.contentType);
88
    if (!document.contentType.includes('json')) {
89
90
      return;
91
92
    }
93
94
    dlog(e);
95
96
    const target = e.target;
97
    if (target.tagName.toUpperCase() == 'I') { // {{{
98
99
      const isClose = target.classList.contains(COLL);
0 ignored issues
show
Bug introduced by
The variable COLL seems to be never declared. If this is a global, consider adding a /** global: COLL */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
100
      const classname = target.classList[0];
101
      if (isClose) {
102
103
        target.removeAttribute('class');
104
        target.setAttribute('class', classname);
105
106
      } else {
107
108
        target.removeAttribute('class');
109
        target.setAttribute('class', `${classname } C${ classname.substring(1)}`);
110
111
      }
112
      e.preventDefault();
113
      // }}}
114
115
    } else if (target.tagName.toUpperCase() == 'STR') { // {{{
116
117
      var parentElement = target.parentElement;
118
      const originStr = parentElement.innerHTML;
119
      parentElement.innerHTML = parentElement.getAttribute('content');
120
      parentElement.setAttribute('content', originStr);
121
      // }}}
122
123
    } else if (target.tagName.toUpperCase() == 'JSON') { // {{{
124
125
      if (document.getElementById('light')) {
126
127
        var lightDiv = document.getElementById('light');
128
        lightDiv.parentElement.removeChild(lightDiv);
129
130
      }
131
      if (document.getElementById('fade')) {
132
133
        var fadeDiv = document.getElementById('fade');
134
        fadeDiv.parentElement.removeChild(fadeDiv);
135
136
      }
137
138
      var parentElement = target.parentElement;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable parentElement already seems to be declared on line 117. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
139
140
      var fadeDiv = document.createElement('div');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable fadeDiv already seems to be declared on line 133. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
141
      fadeDiv.setAttribute('id', 'fade');
142
      fadeDiv.classList.add('black_overlay');
143
      document.body.appendChild(fadeDiv);
144
145
      var lightDiv = document.createElement('div');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable lightDiv already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
146
      lightDiv.setAttribute('id', 'light');
147
      lightDiv.classList.add('white_content');
148
      document.body.appendChild(lightDiv);
149
150
      document.getElementById('light').style.display = 'block';
151
      document.getElementById('fade').style.display = 'block';
152
      draw(eval(parentElement.getAttribute('json')), lightDiv);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
153
      // }}}
154
155
    } else if (target.classList.contains('black_overlay')) { // {{{
156
157
      var fadeDiv = document.getElementById('fade');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable fadeDiv already seems to be declared on line 133. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
158
      fadeDiv.parentElement.removeChild(fadeDiv);
159
      var lightDiv = document.getElementById('light');
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable lightDiv already seems to be declared on line 127. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
160
      lightDiv.parentElement.removeChild(lightDiv);
161
      // }}}
162
163
    } else if (target.className.toUpperCase() == DIV.toUpperCase()) { // {{{
0 ignored issues
show
Bug introduced by
The variable DIV seems to be never declared. If this is a global, consider adding a /** global: DIV */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
164
165
      if (isHighlight) {
166
167
        if (target.style.borderLeft) {
168
169
          target.removeAttribute('style');
170
171
        } else {
172
173
          target.style = 'border-left:1px solid';
174
175
        }
176
177
      }
178
      // }}}
179
180
    } else if (target.tagName.toUpperCase() === 'URL') { // {{{
181
182
      url = target.parentElement.getAttribute('url');
0 ignored issues
show
Bug introduced by
The variable url seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.url.
Loading history...
183
      dlog(`go:${ url}`);
184
      window.open(url);
185
      // }}}
186
187
    }
188
189
  }, true);
190
191
}
192